<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggleDialog" :class="{active}">
      <span :class="{placeholder:!fullLocation}" >{{fullLocation || '请选择配送地址'}}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <div v-if="loading" class="loading"></div>
      <template v-else>
        <span class="ellipsis" v-for="item in currList" :key="item.code" @click="changeItem(item)">{{item.name}}</span>
      </template>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
import { computed, ref, reactive } from 'vue'
import { onClickOutside } from '@vueuse/core'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: '请选择配送地址'
    }
  },
  setup (props, { emit }) {
    const target = ref(null)
    const active = ref(false)
    const loading = ref(false)
    const cityData = ref([])
    const toggleDialog = async () => {
      active.value = !active.value
      if (active.value) {
        loading.value = true
        cityData.value = await getCityData()
        loading.value = false
        // console.log(cityData.value)
        for (const key in changeResult) {
          changeResult[key] = ''
        }
      }
    }
    const getCityData = () => {
      return new Promise((resolve) => {
        if (window.cityData) {
          resolve(window.cityData)
        } else {
          const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
          axios.get(url).then(({ data }) => {
            window.cityData = data
            resolve(window.cityData)
          })
        }
      })
    }
    const currList = computed(() => {
      let temp = cityData.value
      if (changeResult.provinceCode) {
        temp = temp.find(p => p.code === changeResult.provinceCode).areaList
        // console.log(temp)
      }
      if (changeResult.cityCode) {
        temp = temp.find(p => p.code === changeResult.cityCode).areaList
        // console.log(temp)
      }
      return temp
    })
    onClickOutside(target, () => {
      active.value = false
    })
    const changeResult = reactive({
      // 🔔按照添加收货地址的接口文档参数要求准备数据
      // code 用于后端请求，name 是用于前端展示
      provinceCode: '', // 所在省份编码
      provinceName: '', // 所在省份名字
      cityCode: '', // 所在城市编码
      cityName: '',
      countyCode: '', // 所在区/县编码
      countyName: '',
      fullLocation: '' // 完整地址，包括省市区3级的文字
    })
    const changeItem = (theItem) => {
      // console.log(theItem)
      if (theItem.level === 0) {
        changeResult.provinceCode = theItem.code
        changeResult.provinceName = theItem.name
      }
      if (theItem.level === 1) {
        changeResult.cityCode = theItem.code
        changeResult.cityName = theItem.name
      }
      if (theItem.level === 2) {
        changeResult.countyCode = theItem.code
        changeResult.countyName = theItem.name
        changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`
        active.value = false
        emit('change', changeResult)
      }
    }
    return { target, toggleDialog, active, currList, loading, changeItem }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
